﻿using Digitalmes.Model.ThirdParty.FdsWeber;
using Digitalmes.Services.Tenant;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using WalkingTec.Mvvm.Core;

namespace Digitalmes.DataAccess
{
    public class FdsWeberDbContext: EmptyContext
    {
       
        // 存储要动态注册的实体类型
        private  List<Type> _entityTypes;
        public FdsWeberDbContext(string cs) : base(cs)
        {
           
        }

        public FdsWeberDbContext(string cs, DBTypeEnum dbtype, string version = null) : base(cs, dbtype, version)
        {
          
        }

        public FdsWeberDbContext(CS cs) : base(cs)
        {
          
        }

        private readonly ITenantProvider _tenantProvider;

        public FdsWeberDbContext(CS cs, ITenantProvider tenantProvider)
            : base(cs)
        {
            _tenantProvider = tenantProvider;
        }




        // 初始化实体类型（在构造函数中调用）
        private void InitializeEntityTypes()
        {
            // 动态发现实现了 IFdsWeberEntity 的实体
            _entityTypes =typeof(IFdsWeberEntity).Assembly.GetTypes()
                            .Where(t => t.IsClass &&
                             !t.IsAbstract &&
                            typeof(IFdsWeberEntity).IsAssignableFrom(t))
                            .ToList();
        }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            InitializeEntityTypes();
            // 动态注册所有实体类型
            foreach (var entityType in _entityTypes)
            {
                // 使用通用方法动态注册实体
                modelBuilder.Entity(entityType);
            }
        }


        // 通用访问方法
        public IQueryable<T> Query<T>() where T : class
        {
            return Set<T>().AsQueryable();
        }

      

    }
}
